package org.jimmy.tang.ws.axis2;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.log4j.Logger;

/**
 * @author jimmy
 * 
 */
public class HelloClient {

    private final Logger logger = Logger.getLogger(HelloClient.class);

    private RPCServiceClient serviceClient;
    private Options options;
    private EndpointReference targetEPR;

    public HelloClient(String endpoint) throws AxisFault {
        serviceClient = new RPCServiceClient();
        options = serviceClient.getOptions();
        targetEPR = new EndpointReference(endpoint);
        options.setTo(targetEPR);
    }

    public String invokeOp(String targetNamespace, String opName,
            Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
            ClassNotFoundException {

        QName opQName = new QName(targetNamespace, opName);

        Object[] obj = serviceClient.invokeBlocking(opQName, opArgs,
                opReturnType);
        String rs = obj[0].toString();
        logger.debug("Response is : " + rs);
        return rs;
    }

    /**
     * @param args
     * @throws AxisFault
     * @throws ClassNotFoundException
     */
    public static void main(String[] args) throws AxisFault,
            ClassNotFoundException {

        final String endPointReference = "http://localhost:8080/axis2/services/HelloService";
        final String targetNamespace = "http://axis2.webservice.tang.jimmy.org";
        HelloClient client = new HelloClient(endPointReference);

        String opName = "sayHello";
        Object[] opArgs = new Object[] { "Jimmy" };
        Class<?>[] opReturnType = new Class[] { String[].class };
        client.invokeOp(targetNamespace, opName, opArgs, opReturnType);

    }
}
